package com.ylsk.check.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;
import com.google.common.primitives.Longs;
import com.ylsk.check.domain.dto.DeptConsUserDTO;
import com.ylsk.check.domain.so.DeptConsUserSO;
import com.ylsk.check.domain.vo.DeptConsUserVO;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.constant.GlobalConstants;
import com.ylsk.common.core.domain.entity.SysUser;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.DeptConsUserMapper;
import com.ylsk.check.domain.DeptConsUser;
import com.ylsk.check.service.IDeptConsUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 大队施工人员Service业务层处理
 *
 * @author yangsy
 * @date 2024-01-29
 */
@Service
public class DeptConsUserServiceImpl implements IDeptConsUserService
{
    @Autowired
    private DeptConsUserMapper deptConsUserMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询大队施工人员
     *
     * @param userId 大队施工人员主键
     * @return 大队施工人员
     */
    @Override
    public DeptConsUserVO selectDeptConsUserByDeptConsUserId(Long userId)
    {
        SysUser sysUser = userService.selectUserById(userId);
        DeptConsUserVO deptConsUserVO = new DeptConsUserVO();
        BeanUtils.copyProperties(sysUser,deptConsUserVO);
        Map<String,Object> map = Maps.newHashMap();
        map.put("user_id",userId);
        List<DeptConsUser> deptConsUserList = deptConsUserMapper.selectByMap(map);
        if(!CollectionUtils.isEmpty(deptConsUserList)){
            List<Long> deptIds = deptConsUserList.stream().map(deptConsUser -> deptConsUser.getDeptId()).collect(Collectors.toList());
            deptConsUserVO.setDeptIds(deptIds);
        }
        return deptConsUserVO;
    }

    /**
     * 查询大队施工人员列表
     *
     * @param deptConsUserSO 大队施工人员
     * @return 大队施工人员
     */
    @Override
    public List<DeptConsUserVO> selectDeptConsUserList(DeptConsUserSO deptConsUserSO)
    {
        return deptConsUserMapper.selectDeptConsUserList(deptConsUserSO);
    }

    /**
     * 新增大队施工人员
     *
     * @param deptConsUserDTO 大队施工人员
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDeptConsUser(SysUser user,DeptConsUserDTO deptConsUserDTO)
    {
        //设置人员类型
        user.setUserType("01");
        //设置角色  默认资料填写员
        List<Long> roleIds = Longs.asList(GlobalConstants.DATA_FILL_ROLE_ID);
        user.setRoleIds(roleIds.toArray(new Long[roleIds.size()]));
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        userService.insertUser(user);

        //设置人员id
        Long userId = user.getUserId();
        if(null == userId){
            throw new ServiceException("参数异常!人员id!");
        }
        List<Long> deptIds = deptConsUserDTO.getDeptIds();
        if(CollectionUtils.isEmpty(deptIds)){
            throw new ServiceException("参数异常,所属大队为空!");
        }

        int result = 0 ;
        for (Long deptId : deptIds) {
            DeptConsUser deptConsUser = new DeptConsUser(snowIdConfig.getSnowId(),deptId,userId);
            result += deptConsUserMapper.insert(deptConsUser);
        }
        return result;
    }

    /**
     * 修改大队施工人员
     *
     * @param deptConsUserDTO 大队施工人员
     * @return 结果
     */
    @Override
    public int updateDeptConsUser(SysUser user,DeptConsUserDTO deptConsUserDTO)
    {
        Long userId = user.getUserId();
        if(null == userId){
            throw new ServiceException("参数异常!人员id为空");
        }
        List<Long> deptIds = deptConsUserDTO.getDeptIds();
        if(CollectionUtils.isEmpty(deptIds)){
            throw new ServiceException("参数异常,所属大队为空!");
        }
        //更新人员信息
        userService.updateUser(user);
        //更新大队施工人员记录
        Map<String,Object> map = Maps.newHashMap();
        map.put("user_id",userId);
        deptConsUserMapper.deleteByMap(map);

        int result = 0 ;
        for (Long deptId : deptIds) {
            DeptConsUser deptConsUser = new DeptConsUser(snowIdConfig.getSnowId(),deptId,userId);
            result += deptConsUserMapper.insert(deptConsUser);
        }
        return result;
    }

    /**
     * 批量删除大队施工人员
     *
     * @param deptConsUserIds 需要删除的大队施工人员主键
     * @return 结果
     */
    @Override
    public int deleteDeptConsUserByDeptConsUserIds(Long[] deptConsUserIds)
    {
        return deptConsUserMapper.deleteDeptConsUserByDeptConsUserIds(deptConsUserIds);
    }

    /**
     * 删除大队施工人员信息
     *
     * @param deptConsUserId 大队施工人员主键
     * @return 结果
     */
    @Override
    public int deleteDeptConsUserByDeptConsUserId(Long deptConsUserId)
    {
        return deptConsUserMapper.deleteDeptConsUserByDeptConsUserId(deptConsUserId);
    }
}
